#!/usr/bin/env python
# coding:utf-8
import cyclone.auth
import cyclone.escape
import cyclone.web
from toughadmin.console.admin.base import BaseHandler, MenuSys
from toughadmin.tools.permit import permit
from toughadmin.console.admin import config_forms
from toughadmin.console import models
import ConfigParser


class ConfigHandler(BaseHandler):
    @cyclone.web.authenticated
    def get(self):
        active = self.get_argument("active", "default")
        cfg = ConfigParser.ConfigParser()
        cfg.read(self.settings['cfgfile'])
        default_form = config_forms.default_form()
        default_form.fill(dict(cfg.items("DEFAULT")))
        database_form = config_forms.database_form()
        database_form.fill(dict(cfg.items("database")))
        admin_form = config_forms.admin_form()
        admin_form.fill(dict(cfg.items("admin")))

        paramDict = {}
        params = self.db.query(models.TraParam).all()

        for param in params:
            paramDict[param.param_name] = param.param_value


        self.render("config.html",
                  active=active,
                  default_form=default_form,
                  database_form=database_form,
                  admin_form=admin_form
              )


class DefaultHandler(BaseHandler):
    @cyclone.web.authenticated
    def post(self):
        cfg = ConfigParser.ConfigParser()
        cfg.read(self.settings['cfgfile'])
        cfg.set('DEFAULT', 'debug', self.get_argument("debug"))
        cfg.set('DEFAULT', 'tz', self.get_argument("tz"))
        cfg.set('DEFAULT', 'ssl', self.get_argument("ssl"))
        cfg.set('DEFAULT', 'privatekey', self.get_argument("privatekey"))
        cfg.set('DEFAULT', 'certificate', self.get_argument("certificate"))

        with open(self.settings['cfgfile'], 'wb') as configfile:
            cfg.write(configfile)

        self.redirect("/config?active=default")


class DatabaseHandler(BaseHandler):
    @cyclone.web.authenticated
    def post(self):
        cfg = ConfigParser.ConfigParser()
        cfg.read(self.settings['cfgfile'])
        cfg.set('database', 'echo', self.get_argument("echo"))
        cfg.set('database', 'dbtype', self.get_argument("dbtype"))
        cfg.set('database', 'dburl', self.get_argument("dburl"))
        cfg.set('database', 'pool_size', self.get_argument("pool_size"))
        cfg.set('database', 'pool_recycle', self.get_argument("pool_recycle"))
        cfg.set('database', 'backup_path', self.get_argument("backup_path"))

        with open(self.settings['cfgfile'], 'wb') as configfile:
            cfg.write(configfile)

        self.redirect("/config?active=database")

class AdminHandler(BaseHandler):
    @cyclone.web.authenticated
    def post(self):
        cfg = ConfigParser.ConfigParser()
        cfg.read(self.settings['cfgfile'])
        cfg.set('admin', 'host', self.get_argument("host"))
        cfg.set('admin', 'port', self.get_argument("port"))
        cfg.set('admin', 'logfile', self.get_argument("logfile"))
        cfg.set('admin', 'policy_server', self.get_argument("policy_server"))
        cfg.set('admin', 'policy_port', self.get_argument("policy_port"))

        with open(self.settings['cfgfile'], 'wb') as configfile:
            cfg.write(configfile)

        self.redirect("/config?active=admin")


permit.add_route(ConfigHandler, r"/config", u"参数配置管理", u"系统管理", order=1.0100, is_menu=True)
permit.add_route(DefaultHandler, r"/config/default/update", u"默认配置", u"系统管理", order=1.0101, is_menu=False)
permit.add_route(DatabaseHandler, r"/config/database/update", u"数据库配置", u"系统管理", order=1.0102, is_menu=False)
permit.add_route(AdminHandler, r"/config/admin/update", u"管理配置", u"系统管理", order=1.0103, is_menu=False)